<template>
  <div class="container">
    <div class="header">
      <el-button type="primary" @click="openAddForm(1)">新增菜品分类</el-button>
      <el-button type="success" @click="openAddForm(2)">新增套餐分类</el-button>
    </div>

    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="name" label="分类名称"></el-table-column>
      <el-table-column prop="type" label="分类类型">
        <template v-slot="{ row }">
          {{ row.type === 1 ? '菜品分类' : '套餐分类' }}
        </template>
      </el-table-column>
      <el-table-column prop="updateTime" label="操作时间"></el-table-column>
      <el-table-column prop="sort" label="排序"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-link type="primary" @click="openEditForm(row)">编辑</el-link>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm
            title="确认删除该分类?"
            @onConfirm="handleDelete(row)"
            :disabled="row.isAssociated"
          >
            <el-link type="danger" slot="reference" :disabled="row.isAssociated">
              删除
            </el-link>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div class="footer">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageInfo.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <!-- 添加分类表单 -->
    <add-category-form ref="addForm" @submit="handleAddCategory" />
    <!-- 编辑分类表单 -->
    <edit-category-form ref="editForm" @submit="handleEditCategory" />
  </div>
</template>

<script>
import { getCategories, addCategory, updateCategory, deleteCategory } from "@/api/category";
import AddCategoryForm from './AddCategoryForm.vue';
import EditCategoryForm from './EditCategoryForm.vue';

export default {
  components: {
    AddCategoryForm,
    EditCategoryForm,
  },
  data() {
    return {
      tableData: [],
      pageInfo: { page: 1, pageSize: 10 },
      total: 0,
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      getCategories(this.pageInfo).then((res) => {
        this.tableData = res.data.records.map((item) => ({
          ...item,
          isAssociated: item.isAssociated, // 是否关联了套餐或菜品
        }));
        this.total = res.data.total;
      });
    },
    handleSizeChange(val) {
      this.pageInfo.page = 1;
      this.pageInfo.pageSize = val;
      this.init();
    },
    handleCurrentChange(val) {
      this.pageInfo.page = val;
      this.init();
    },
    openAddForm(type) {
      this.$refs.addForm.open(type); // 打开添加分类表单，传入分类类型
    },
    openEditForm(row) {
      this.$refs.editForm.open(row); // 打开编辑分类表单
    },
    async handleAddCategory(form) {
      try {
        const res = await addCategory(form);
        if (res.code === 200) {
          this.$message.success("添加分类成功");
          this.init();
        }
      } catch (error) {
        this.$message.error("添加分类失败");
      }
    },
    async handleEditCategory(form) {
      try {
        const res = await updateCategory(form);
        if (res.code === 200) {
          this.$message.success("编辑分类成功");
          this.init();
        }
      } catch (error) {
        this.$message.error("编辑分类失败");
      }
    },
    async handleDelete(row) {
      if (row.isAssociated) {
        this.$message.warning("该分类关联了套餐或菜品，不能删除");
        return;
      }
      try {
        const res = await deleteCategory({ id: row.id });
        if (res.code === 200) {
          this.$message.success("删除分类成功");
          this.init();
        }
      } catch (error) {
        this.$message.error("删除分类失败");
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  padding: 33px;
  .header {
    margin-bottom: 20px;
  }
  .el-table {
    margin-top: 22px;
  }
  .footer {
    margin-top: 30px;
    display: flex;
    justify-content: center;
  }
}
</style>